package com.wzt.lianfirecontrol.activity;

import android.content.Intent;
import android.net.Uri;
import android.nfc.NfcAdapter;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.mob.pushsdk.MobPush;
import com.wzt.lianfirecontrol.R;
import com.wzt.lianfirecontrol.adapter.message.MessageListAdapter;
import com.wzt.lianfirecontrol.bean.BaseModel;
import com.wzt.lianfirecontrol.bean.ConstData;
import com.wzt.lianfirecontrol.bean.recode.BannerModel;
import com.wzt.lianfirecontrol.fragment.BarFloorFragment;
import com.wzt.lianfirecontrol.fragment.BarSelectFragment;
import com.wzt.lianfirecontrol.fragment.BarTitleFragment;
import com.wzt.lianfirecontrol.fragment.BaseFragment;
import com.wzt.lianfirecontrol.fragment.DeviceDetailFragment;
import com.wzt.lianfirecontrol.fragment.InfoListFragment;
import com.wzt.lianfirecontrol.fragment.baojingfuhe.FuHeDetailFragment;
import com.wzt.lianfirecontrol.fragment.baojingfuhe.WuBaoFragment;
import com.wzt.lianfirecontrol.fragment.jinji.AddJinJiWeiXiuFragment;
import com.wzt.lianfirecontrol.fragment.jinji.JinJiWeiXiuDetailFragment;
import com.wzt.lianfirecontrol.fragment.message.MessageDetailFragment;
import com.wzt.lianfirecontrol.fragment.my.AdviceBackFragment;
import com.wzt.lianfirecontrol.fragment.my.ChangeInfoFragment;
import com.wzt.lianfirecontrol.fragment.my.ContactKeFuFragment;
import com.wzt.lianfirecontrol.fragment.my.MyTeamFragment;
import com.wzt.lianfirecontrol.fragment.my.SettingFragment;
import com.wzt.lianfirecontrol.fragment.xiaofangtongdao.XftdFragment;
import com.wzt.lianfirecontrol.fragment.xiaokongshi.AddUnusualFragment;
import com.wzt.lianfirecontrol.fragment.xiaokongshi.DutyDetailFragment;
import com.wzt.lianfirecontrol.fragment.xiaokongshi.DutyHistoryFragment;
import com.wzt.lianfirecontrol.fragment.xiaokongshi.DutyListFragment;
import com.wzt.lianfirecontrol.fragment.xiaokongshi.UnusualDetailFragment;
import com.wzt.lianfirecontrol.fragment.xuncha.AddXunChaQueFragment;
import com.wzt.lianfirecontrol.fragment.xuncha.StartXunChaFragment;
import com.wzt.lianfirecontrol.fragment.xuncha.XunChaCompleteFragment;
import com.wzt.lianfirecontrol.fragment.xuncha.XunChaHistoryFragment;
import com.wzt.lianfirecontrol.fragment.xuncha.XunChaQueDetailFragment;
import com.wzt.lianfirecontrol.fragment.xuncha.xunchadevice.AddXunChaDeviceQueFragment;
import com.wzt.lianfirecontrol.fragment.xuncha.xunchadevice.XunChaDeviceQueDetailFragment;
import com.wzt.lianfirecontrol.fragment.yinghuan.AddYingHuanFragment;
import com.wzt.lianfirecontrol.fragment.yinghuan.AddYingHuanRecordFragment;
import com.wzt.lianfirecontrol.fragment.yinghuan.YingHuanDetailFragment;
import com.wzt.lianfirecontrol.fragment.yuyueweibao.AddYuYueFragment;
import com.wzt.lianfirecontrol.fragment.yuyueweibao.AddYuYueRecordFragment;
import com.wzt.lianfirecontrol.fragment.yuyueweibao.YuYueDetailFragment;
import com.wzt.lianfirecontrol.fragment.yuyueweibao.YuYueEvaluateFragment;
import com.wzt.lianfirecontrol.utils.DialogUtils;
import com.wzt.lianfirecontrol.utils.GetPhotoUtils;
import com.wzt.lianfirecontrol.utils.GildeUtils;
import com.wzt.lianfirecontrol.utils.StringUtils;
import com.wzt.lianfirecontrol.utils.ToastUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class ShowFragActivity extends BaseActivity implements GetPhotoUtils.AddImageAction {

    private Bundle bundle;

    private View include_head_title;
    private ImageView iv_back;
    public TextView tv_title;
    private TextView tv_edit;
    private ImageView iv_edit;
    private ImageView iv_change_cad;
    private TextView tv_check_nfc;

    private BaseModel skipModel;
    private BaseFragment baseFragment;

    private ImageView iv_show_img;

    // 上传图片弹出菜单
    private PopupWindow upImgPopupWindow;
    private Uri photoUri;
    private static final int IMAGE_OPEN = 0;
    private static final int PHOTO_OPEN = 1;

    private int currentPhotoWhat = -1;

    public static final int OPEN_NFC_WHAT = 2;
    public Bundle skipToNfcBundle;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        bundle = getIntent().getExtras();
        if (null != bundle){
            skipModel = (BaseModel) bundle.getSerializable(ConstData.ACTIONMODLE);
        }
        if (null == skipModel){
            ToastUtils.showToast(this, getResources().getString(R.string.data_error));
            finish();
            return;
        }
        setContentView(R.layout.a_show_frag);
        setImmerseLayout(findViewById(R.id.include_head_title), findViewById(R.id.rl_head_content));
        initView();
    }

    private void initView() {
        iniHeadTitle();
        initFragment();
        iv_show_img = findViewById(R.id.iv_show_img);
        iv_show_img.setVisibility(View.GONE);
        iv_show_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                iv_show_img.setVisibility(View.GONE);
            }
        });
    }

    private void iniHeadTitle() {
        include_head_title = findViewById(R.id.include_head_title);
        include_head_title.setBackground(getResources().getDrawable(R.drawable.shape_head_title_bg_frame));
        iv_back = findViewById(R.id.iv_back);
        iv_back.setImageResource(R.drawable.back_white);
        iv_back.setVisibility(View.VISIBLE);
        tv_title = findViewById(R.id.tv_title);
        tv_title.setTextColor(getResources().getColor(R.color.white));
        tv_edit = findViewById(R.id.tv_edit);
        tv_edit.setTextColor(getResources().getColor(R.color.white));
        iv_edit = findViewById(R.id.iv_edit);
        iv_change_cad = findViewById(R.id.iv_change_cad);
        tv_check_nfc = findViewById(R.id.tv_check_nfc);

        findViewById(R.id.v_divdier_line).setVisibility(View.GONE);

        if (StringUtils.isEmpty(skipModel.getActionTitle())){
            tv_title.setText("");
            include_head_title.setVisibility(View.GONE);
        }else {
            tv_title.setText(skipModel.getActionTitle());
            include_head_title.setVisibility(View.VISIBLE);
        }
    }

    private void initFragment() {
        switch (skipModel.getActionValue()){
            case ConstData.MESSAGEDETAIL:// 消息详情
                baseFragment = new MessageDetailFragment();
                break;
            case ConstData.BAOJINGDETAIL:// 报警详情
                baseFragment = new DeviceDetailFragment();
                break;
            case ConstData.BARTITLE:// 故障屏蔽选择
                baseFragment = new BarTitleFragment();
                break;
            case ConstData.BARFLOOR:// 楼层选择滑动
                baseFragment = new BarFloorFragment();
                break;
            case ConstData.BARSELECT:// 筛选
                baseFragment = new BarSelectFragment();
                break;
            case ConstData.CHANGEINFO:// 编辑用户信息
                baseFragment = new ChangeInfoFragment();
                break;
            case ConstData.SETTING:// 设置
                baseFragment = new SettingFragment();
                break;
            case ConstData.MYTEAM:// 我的团队
                baseFragment = new MyTeamFragment();
                break;
            case ConstData.CONTACTKEFU:// 联系客服
                baseFragment = new ContactKeFuFragment();
                break;
            case ConstData.ADVICEBACK:// 意见反馈
                baseFragment = new AdviceBackFragment();
                break;
            case ConstData.DUTYLIST:// 消控室值班
                baseFragment = new DutyListFragment();
                break;
            case ConstData.DUTYDETAIL:// 消控室值班详情
                baseFragment = new DutyDetailFragment();
                break;
            case ConstData.ADDUNUSUAL:// 添加异常
                baseFragment = new AddUnusualFragment();
                break;
            case ConstData.UNUSUALDETAIL:// 异常详情
                baseFragment = new UnusualDetailFragment();
                break;
            case ConstData.DUTYHISTORY:// 值班历史
                baseFragment = new DutyHistoryFragment();
                break;
            case ConstData.FUHEDETAIL:// 复核详情
                baseFragment = new FuHeDetailFragment();
                break;
            case ConstData.WUBAO:// 误报处理
                baseFragment = new WuBaoFragment();
                break;
            case ConstData.STARTXUNCHA:// 开始巡检
                baseFragment = new StartXunChaFragment();
                break;
            case ConstData.XUNCHADETAIL:// 防火问题详情
                baseFragment = new XunChaQueDetailFragment();
                break;
            case ConstData.XUNCHADEVICEDETAIL:// 设备问题详情
                baseFragment = new XunChaDeviceQueDetailFragment();
                break;
            case ConstData.XUNCHHISTORY:// 巡查历史
                baseFragment = new XunChaHistoryFragment();
                break;
            case ConstData.XUNCHACOMPLETE:// 巡查已完成
                baseFragment = new XunChaCompleteFragment();
                break;
            case ConstData.ADDXUNCHAQES:// 添加巡查问题
                baseFragment = new AddXunChaQueFragment();
                break;
            case ConstData.ADDXUNCHADETAILQES:// 添加巡查问题
                baseFragment = new AddXunChaDeviceQueFragment();
                break;
            case ConstData.YUYUEDETAIL:// 预约维保详情
                baseFragment = new YuYueDetailFragment();
                break;
            case ConstData.ADDYUYUE:// 添加预约保单
                baseFragment = new AddYuYueFragment();
                break;
            case ConstData.JINJIDETAIL:// 紧急维修单详情
                baseFragment = new JinJiWeiXiuDetailFragment();
                break;
            case ConstData.ADDJINJI:// 添加紧急维修单
                baseFragment = new AddJinJiWeiXiuFragment();
                break;
            case ConstData.YINGHUANDETAIL:// 隐患整改详情
                baseFragment = new YingHuanDetailFragment();
                break;
            case ConstData.ADDYINGHUAN:// 添加隐患整改
                baseFragment = new AddYingHuanFragment();
                break;
            case ConstData.XFTD:// 消防通道
                baseFragment = new XftdFragment();
                break;
            case ConstData.ADDYUYUERECORD:// 添加维保记录
                baseFragment = new AddYuYueRecordFragment();
                break;
            case ConstData.ADDYINGHUANRECORD:
                baseFragment = new AddYingHuanRecordFragment();
                break;
            case ConstData.EVALUATE:
                baseFragment = new YuYueEvaluateFragment();
                break;
            case ConstData.INFOLIST:// 信息流列表
            default:
                baseFragment = new InfoListFragment();
                break;
        }
        baseFragment.setArguments(bundle);
        addFragment(R.id.fl_container, baseFragment);
    }

    /**
     * 设置右上角点击动作
     * @param bannerModel
     */
    public void setTextEtAction(final BannerModel bannerModel, final Bundle bundle){
        if (null != tv_edit){
            if (null != bannerModel) {
                tv_edit.setText(bannerModel.getTitle());
                tv_edit.setVisibility(View.VISIBLE);
                tv_edit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        ShowFragActivity.this.setClickAction(bannerModel, bundle);
                    }
                });
            }else {
                tv_edit.setVisibility(View.GONE);
            }
        }
    }

    /**
     * 设置右上角点击动作
     * @param clickListener
     */
    public void setTextEtAction(String title, View.OnClickListener clickListener){
        if (null != tv_edit){
            tv_edit.setText(title);
            tv_edit.setVisibility(View.VISIBLE);
            tv_edit.setOnClickListener(clickListener);
        }
    }

    /**
     * 设置右上角点击动作
     * @param imageResource
     */
    public void setImageEtAction(int imageResource, View.OnClickListener onClickListener){
        if (null != iv_edit){
            if (0 != imageResource) {
                iv_edit.setImageResource(imageResource);
                iv_edit.setVisibility(View.VISIBLE);
                iv_edit.setOnClickListener(onClickListener);
            }else {
                iv_edit.setVisibility(View.GONE);
            }
        }
    }

    /**
     * 设置nfc巡查动作
     * @param onClickListener
     */
    public void setNfcCheckAction(View.OnClickListener onClickListener){
        if (null != tv_check_nfc){
            tv_check_nfc.setVisibility(View.VISIBLE);
            tv_check_nfc.setOnClickListener(onClickListener);
        }
    }


    /**
     * 设置右上角点击动作
     * @param
     */
    public void setImageChangeAction(View.OnClickListener onClickListener){
        if (null != iv_change_cad){
            iv_change_cad.setVisibility(View.VISIBLE);
            iv_change_cad.setOnClickListener(onClickListener);
        }
    }

    /**
     * 显示上传图片弹窗
     */
    public void initSubmitPicView(final int photoWhat) {
        currentPhotoWhat = photoWhat;
        View parentView = getWindow().getDecorView();
        if (null == upImgPopupWindow) {
            List<String> strList = new ArrayList<>();
            strList.add("拍照");
            strList.add("本地相册");
            upImgPopupWindow = DialogUtils.createListLikeIosPopupWindow(this, (ViewGroup) parentView, strList, new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    upImgPopupWindow.dismiss();
                    switch (position) {
                        case 0:
                            skipToPlayPhoto(currentPhotoWhat);
                            break;
                        case 1:
                            skipToLocalImg(currentPhotoWhat);
                            break;
                        default:
                            break;
                    }
                }
            }, "取消", new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    DialogUtils.closePopUpWindow(upImgPopupWindow);
                }
            }, "");
            upImgPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                    DialogUtils.darkenBackground(1f, ShowFragActivity.this);
                }
            });

        }
        if (!upImgPopupWindow.isShowing()) {
            DialogUtils.darkenBackground(0.5f, this);
            upImgPopupWindow.showAtLocation((ViewGroup) parentView, Gravity.BOTTOM, 0, 0);
        }
    }

    /**
     * 跳转到拍照
     * @param photoWhat
     */
    public void skipToPlayPhoto(int photoWhat){
        currentPhotoWhat = photoWhat;
        //只是加了一个uri作为地址传入
        File photoFile = GetPhotoUtils.createImgFile(ShowFragActivity.this);
        if (null == photoFile) {
            ToastUtils.showToast(ShowFragActivity.this, "本机不支持该功能，请从相册上传图片");
            return;
        }
        photoUri = Uri.fromFile(photoFile);
        GetPhotoUtils.skipToPhoto(ShowFragActivity.this, PHOTO_OPEN, photoUri);
    }

    /**
     * 跳转到本地相册
     * @param photoWhat
     */
    public void skipToLocalImg(int photoWhat){
        currentPhotoWhat = photoWhat;
        GetPhotoUtils.skipToLocalImages(ShowFragActivity.this, IMAGE_OPEN);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case IMAGE_OPEN:// 相册
                    Uri uri = data.getData();
                    GetPhotoUtils.dealWithAddImageAction(ShowFragActivity.this, uri, ShowFragActivity.this, currentPhotoWhat, false);
                    currentPhotoWhat = -1;
                    break;
                case PHOTO_OPEN:// 拍照
                    GetPhotoUtils.dealWithAddImageAction(ShowFragActivity.this, photoUri, ShowFragActivity.this, currentPhotoWhat, true);
                    currentPhotoWhat = -1;
                    break;
            }
        } else if (requestCode == OPEN_NFC_WHAT){
            NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
            if (nfcAdapter == null) {
                ToastUtils.showToast(this, "此设备不支持nfc功能");
                return;
            }
            if (!nfcAdapter.isEnabled()) {
                ToastUtils.showToast(this, "nfc功能未打开");
                return;
            }
            if (null == skipToNfcBundle){
                skipToNfcBundle = new Bundle();
            }
            openActivity(NfcDeviceActivity.class, skipToNfcBundle, 0);
        }
    }

    @Override
    public void imgSuccessAction(File file, String imageStr, int photoWhat) {
        if (StringUtils.isEmpty(imageStr)){
            ToastUtils.showToast(this, "图片为空");
            return;
        }
        baseFragment.imgSuccessAction(file, imageStr, photoWhat);
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobPush.setBadgeCounts(0);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        DialogUtils.closePopUpWindow(upImgPopupWindow);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK){
            if (baseFragment instanceof InfoListFragment ) {
                InfoListFragment infoListFragment = (InfoListFragment) baseFragment;
                if (ConstData.MESSAGELIST.equals(infoListFragment.showType)
                        && ((MessageListAdapter) infoListFragment.baseRecyclerAdapter).isChooseDelete()) {
                    ((MessageListAdapter) infoListFragment.baseRecyclerAdapter).setChooseDelete(false);
                    infoListFragment.ll_message_bottom.setVisibility(View.GONE);
                    return false;
                }
            }
            if (null != iv_show_img && iv_show_img.getVisibility() == View.VISIBLE) {
                iv_show_img.setVisibility(View.GONE);
                return false;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void doBack(View view) {
        if (baseFragment instanceof InfoListFragment ) {
            InfoListFragment infoListFragment = (InfoListFragment) baseFragment;
            if (ConstData.MESSAGELIST.equals(infoListFragment.showType)
                    && ((MessageListAdapter) infoListFragment.baseRecyclerAdapter).isChooseDelete()) {
                ((MessageListAdapter) infoListFragment.baseRecyclerAdapter).setChooseDelete(false);
                infoListFragment.ll_message_bottom.setVisibility(View.GONE);
                return;
            }
        }
        if (null != iv_show_img && iv_show_img.getVisibility() == View.VISIBLE) {
            iv_show_img.setVisibility(View.GONE);
            return;
        }
        super.doBack(view);;
    }

    /**
     * 设置图片显示
     */
    public void setShowImg(String imgStr){
        if (null != iv_show_img){
            GildeUtils.loadImage(this, iv_show_img, imgStr);
            iv_show_img.setVisibility(View.VISIBLE);
        }
    }

}
